package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.OrderDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_order_detail_aty.*

class OrderDetailAty : BaseAty() {
    override fun getContentId(): Int {
        return R.layout.activity_order_detail_aty
    }
    private var moeny=""
    override fun initView() {

        OkGo.post<OrderDetailBean>(Urls.BaseUrl+Urls.ORDER_DETAIL)
                .params("id",intent.getIntExtra("oid",0))
                .execute(object :JsonCallBack<OrderDetailBean>(this){
                    override fun onSuccess(response: Response<OrderDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            orderNum.text=bean?.data.order_sn
                            orderStatus.text=when(bean?.data.shoping_status){
                                0->"未支付"
                                10->"配货中"
                                20->"已发货"
                                30->"已收货"
                                else->""
                            }
                            if (bean?.data.money!=null){
                                if (bean?.data.money.equals("0")){
                                    price.text="${bean?.data.score}积分"
                                }else{
                                    price.text="￥${bean?.data.money}"
                                }
                                moeny=bean?.data.money.toString()
                            }else{
                                moeny=""
                            }
                            if (!TextUtils.isEmpty(bean?.data.goods.title))itemTittle.text=bean?.data.goods.title
                            if (!TextUtils.isEmpty(bean?.data.goods.img)) Glide.with(this@OrderDetailAty).load(bean?.data.goods.img).asBitmap().into(itemIcon)
                            if (!TextUtils.isEmpty(bean?.data.goods.author))itemAuther.text=bean?.data.goods.author
                            if (!TextUtils.isEmpty(bean?.data.number.toString()))itemNum.text="共${bean?.data.number.toString()}件商品"
                            if (bean?.data.address!=null){

                                if (!TextUtils.isEmpty(bean?.data.address.province)&&!TextUtils.isEmpty(bean?.data.address.city)&&!TextUtils.isEmpty(bean?.data.address.area)
                                        &&!TextUtils.isEmpty(bean?.data.address.address)){
                                    tvAddress.text=bean?.data.address.province+bean?.data.address.city+bean?.data.address.area+bean?.data.address.address
                                }
                                if (!TextUtils.isEmpty(bean?.data.address.name))buyerName.text=bean?.data.address.name
                                if (!TextUtils.isEmpty(bean?.data.address.mobile))tvPhone.text=bean?.data.address.mobile
                            }
                            setClick()
                        }
                    }
                })
    }

    fun setClick(){
        when(orderStatus.text){
            "未支付"->{
                btCommit.visibility=View.VISIBLE
                btCommit.text="立即支付"
                btCommit.setOnClickListener {
                    val intent = Intent(this@OrderDetailAty, OrderPayAty::class.java)
                    intent.putExtra("moeny",moeny)
                    intent.putExtra("order",intent.getIntExtra("oid",0).toString())
                    startActivity(intent)
                }
            }
            "配货中"->{
                btCommit.visibility=View.VISIBLE
                btCommit.text="确认收货"
                btCommit.setOnClickListener {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.COMfirm)
                            .params("id",intent.getIntExtra("oid",0))
                            .execute(object :JsonCallBack<BaseBean>(this@OrderDetailAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("操作成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }
            }
            "已发货"->{
                btCommit.visibility=View.VISIBLE
                btCommit.text="确认收货"
                btCommit.setOnClickListener {
                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.COMfirm)
                            .params("id",intent.getIntExtra("oid",0))
                            .execute(object :JsonCallBack<BaseBean>(this@OrderDetailAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("操作成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })
                }
            }
            "已收货"->{
                btCommit.visibility=View.VISIBLE
                btCommit.text="确认收货"
                btCommit.setOnClickListener {
                    /*OkGo.post<BaseBean>(Urls.BaseUrl+Urls.COMfirm)
                            .params("id",intent.getIntExtra("oid",0))
                            .execute(object :JsonCallBack<BaseBean>(this@OrderDetailAty){
                                override fun onSuccess(response: Response<BaseBean>?) {
                                    val bean = response?.body()
                                    if (bean?.code==1){
                                        MyUtils.toast("操作成功")
                                        finish()
                                    }else{
                                        MyUtils.toast(bean?.msg)
                                    }
                                }
                            })*/
                }
            }
        }
    }
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
    }
}
